﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace HRUI
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            //产生空容器
            var builder = new ContainerBuilder();
            // 获取所有相关类库的程序集
            Assembly[] assemblies = new Assembly[] { Assembly.Load("HRDAO"), Assembly.Load("HRBLL") };
            builder.RegisterAssemblyTypes(assemblies) //注册程序集
          .Where(y => !y.IsAbstract)//不是抽象类
          .AsImplementedInterfaces()//注册所有实现接口
          .PropertiesAutowired(); //自动注入 (必须为属性)

            //将本程序所有控制器全部注册
            builder.RegisterControllers(typeof(MvcApplication).Assembly)
                     .PropertiesAutowired();
            var container = builder.Build();
            //将当前容器的控制器工厂替换掉默认的控制器工厂,(即:不要MVC默认的控制器工厂,用autofac容器中的控制器替代)
            //DefaultControllerFactory  mvc默认的控制器工厂
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}
